package com.gesee.zyspps.utils;

import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;

/**
 * 获取应用程序或本机的信息
 * 
 * @author yuweichen
 * 
 */
@SuppressLint("DefaultLocale")
public class DeviceUtil {
	private static DeviceUtil telephoneUtil = null;
	private Context context;

	private DeviceUtil(Context context) {
		this.context = context;
	}

	public static DeviceUtil getInstance(Context context) {
		if (telephoneUtil == null) {
			telephoneUtil = new DeviceUtil(context);
		}
		return telephoneUtil;
	}

	/**
	 * 获取用户mac地址
	 */
	
	public  String getMacAddress(){
		WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		 
		WifiInfo info = wifi.getConnectionInfo();
		 
		return info.getMacAddress();
	}
	
	
	/**
	 * 获取用户device_id
	 * @return
	 */
	public String getDeviceId() {
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getDeviceId();
	}
	
	/**
	 * 获取App安装包信息
	 * 
	 * @return
	 */
	public PackageInfo getPackageInfo() {
		PackageInfo info = null;
		try {
			info = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace(System.err);
		}
		if (info == null)
			info = new PackageInfo();
		return info;
	}
	
	/**
	 * 获取客户端所在终端信息，用来提供访问参数
	 * @return
	 */
	public String getUserAgentForRequest() {
		String appUserAgent = "";
		if (appUserAgent == null || appUserAgent == "") {
			StringBuilder ua = new StringBuilder();
			ua.append("android Product Model: " + android.os.Build.MODEL + "," + android.os.Build.VERSION.SDK_INT + "," + android.os.Build.VERSION.RELEASE);
			appUserAgent = ua.toString();
		}
		return appUserAgent;
	}
	
	
	/**
	 * 获取客户端所在终端信息，用来打印错误日志
	 * 
	 * @return
	 */
	/*public String getUserAgentForError() {
		String appUserAgent = "";
		if (appUserAgent == null || appUserAgent == "") {
			StringBuilder ua = new StringBuilder();
			ua.append("\n"+"应用名称：" + context.getResources().getString(R.string.app_name)+"\n");
			ua.append("版本编号：" + getPackageInfo().versionName + "\n"
					+ "代码编号：" + getPackageInfo().versionCode+"\n");// App版本
			ua.append("使用平台：" + "Android"+"\n");// 手机系统平台
			ua.append("系统版本：" + android.os.Build.VERSION.RELEASE+"\n");// 手机系统版本
			ua.append("手机品牌：" + android.os.Build.MANUFACTURER+"\n");//手机名称
			ua.append("手机型号：" + android.os.Build.MODEL+"\n"); // 手机型号
			ua.append("唯一标识：" + getAppId()+"\n");//客户端唯一标识
			ua.append("客户端渠道号:"+AppConfig.SOURCE+"\n");
			appUserAgent = ua.toString();
		}
		return appUserAgent;
	}*/

	public String addHttpHeader(){
		String appUserAgent = "";
		if(appUserAgent==null ||  appUserAgent == ""){
			StringBuilder ua = new StringBuilder();
		/*	ua.append("device_name:"+android.os.Build.MANUFACTURER+";");
			ua.append("device_model:"+android.os.Build.MODEL+";");
			ua.append("plamform:"+"Android"+";");
			ua.append("plamform_version:"+android.os.Build.VERSION.RELEASE+";");
			ua.append("device_id:"+ getAppId()+";");*/
		
			ua.append(android.os.Build.MANUFACTURER+";");
			ua.append(android.os.Build.MODEL+";");
			ua.append("Android"+";");
			ua.append(android.os.Build.VERSION.RELEASE+";");
			ua.append(getAppId()+";");
			
			appUserAgent = ua.toString();
		}
		return appUserAgent;
	}
	
	/**
	 * 生产客户端唯一标示
	 * @return
	 */
	public String getAppId() {
		String szImei = "";
		String m_szDevIDShort = "35" + Build.BOARD.length() % 10
				+ Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 10
				+ Build.DEVICE.length() % 10 + Build.DISPLAY.length() % 10
				+ Build.HOST.length() % 10 + Build.ID.length() % 10
				+ Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10
				+ Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10
				+ Build.TYPE.length() % 10 + Build.USER.length() % 10; // 13
		
		String m_szLongID = szImei + m_szDevIDShort + m_szDevIDShort; // compute
		
		MessageDigest m = null;
		
		try {
			m = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		m.update(m_szLongID.getBytes(), 0, m_szLongID.length()); // get md5 //
																	// bytes
		byte p_md5Data[] = m.digest(); // create a hex string
		String m_szUniqueID = new String();
		for (int i = 0; i < p_md5Data.length; i++) {
			int b = (0xFF & p_md5Data[i]); // if it is a single digit, make sure
											// it have 0 in front (proper//
											// padding)
			if (b <= 0xF)
				m_szUniqueID += "0"; // add number to string
			m_szUniqueID += Integer.toHexString(b);
		} // hex string to uppercase 774679D4574925505F3926E1882DB498
		return m_szUniqueID.toUpperCase();
	}
	
	
	/**
	 * 判断当前版本是否兼容目标版本的方法
	 * @param VersionCode
	 * @return
	 */
	public static boolean isMethodsCompat(int VersionCode) {
		int currentVersion = android.os.Build.VERSION.SDK_INT;
		return currentVersion >= VersionCode;
	}
	
	/*public String lastExitDate(){
		return DateUtil.getStringCurrentDateYYMMDD();
	}*/
	
	/**
	 * 判断是否有smartBar
	 * @return
	 */
	public static boolean hasSmartBar(){
		try {
			Method method = Class.forName("android.os.Build").getMethod("hasSmartBar");
			return ((Boolean) method.invoke(null)).booleanValue();
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(Build.DEVICE.equals("mx")||Build.DEVICE.equals("m9")){
			return false;
		}else {
			return true;
		}
	}
}
